package cn.iocoder.yudao.module.trade.scan.controller.app;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.order.pojo.vo.OrderSubmitVO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.idempotent.core.annotation.Idempotent;
import cn.iocoder.yudao.framework.idempotent.core.keyresolver.impl.UserIdempotentKeyResolver;
import cn.iocoder.yudao.module.api.shop.staff.ShopStaffApi;
import cn.iocoder.yudao.module.api.shop.staff.vo.ShopStaffVO;
import cn.iocoder.yudao.module.trade.scan.service.AppScanOrderService;
import cn.iocoder.yudao.module.trade.scan.vo.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 线下扫码订单")
@RestController
@RequestMapping("/trade/order/offline-scan")
@Validated
public class AppScanOrderController {
    @Resource
    private AppScanOrderService appScanOrderService;
    @Resource
    private ShopStaffApi shopStaffApi;

    @GetMapping("/check-customer")
    @Operation(summary = "检查是否新客")
    @Parameter(name = "shopId", description = "店铺编号", required = true)
    @PermitAll
    public CommonResult<NewCustomerCheckVO> checkCustomer(@RequestParam("shopId") Long shopId) {
        return success(appScanOrderService.checkCustomer(shopId, getLoginUserId()));
    }

    @PostMapping("/preview")
    @Operation(summary = "计算价格明细", description = "扫码下单时，每当输入的价格变动则调用本接口计算价格明细")
    @PermitAll // 通过云闪付、支付宝等APP扫码下单只能打开H5页面，允许不登录
    public CommonResult<ScanOrderPreviewVO> preview(@Valid @RequestBody ScanOrderSubmitDTO param) {
        ScanOrderPreviewVO respVO = appScanOrderService.preview(param, getLoginUserId());
        return success(respVO);
    }

    @Idempotent(timeout = 5, message = "重复下单，请稍候再试", keyResolver = UserIdempotentKeyResolver.class)
    @PostMapping("/submit")
    @Operation(summary = "下单", description = "线下扫码下单记录，返回交易单号")
    @PermitAll // 通过云闪付、支付宝等APP扫码下单只能打开H5页面，允许不登录
    public CommonResult<OrderSubmitVO> submit(@Valid @RequestBody ScanOrderSubmitDTO param) {
        OrderSubmitVO respVO = appScanOrderService.submit(param, getLoginUserId());
        return success(respVO);
    }

    @Idempotent(timeout = 5, message = "重复下单，请稍候再试", keyResolver = UserIdempotentKeyResolver.class)
    @PostMapping("/submit-new-customer")
    @Operation(summary = "下单", description = "线下扫码下单记录，返回交易单号")
    @PermitAll // 通过云闪付、支付宝等APP扫码下单只能打开H5页面，允许不登录
    public CommonResult<OrderSubmitVO> submitForNewCustomer(@Valid @RequestBody NewCustomerSubmitDTO param) {
        OrderSubmitVO respVO = appScanOrderService.submitForNewCustomer(param, getLoginUserId());
        return success(respVO);
    }

    @Operation(summary = "分页")
    @GetMapping("/page")
    public CommonResult<PageResult<AppScanOrderVO>> getPage(@Valid PageParam param) {
        return success(appScanOrderService.getPage(param.getPageNo(), param.getPageSize(), getLoginUserId()));
    }

    @Operation(summary = "分页")
    @GetMapping("/page-for-shop")
    public CommonResult<PageResult<AppScanOrderVO>> getPageForShop(@Valid PageParam param) {
        List<ShopStaffVO> shopStaffVOS = shopStaffApi.getByMemberUserId(getLoginUserId());
        if (CollUtil.isEmpty(shopStaffVOS)) {
            return success(PageResult.empty());
        }
        Set<Long> shopIds = shopStaffVOS.stream().map(ShopStaffVO::getShopId).collect(Collectors.toSet());
        return success(appScanOrderService.getPageForShop(param.getPageNo(), param.getPageSize(), shopIds));
    }

}